﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using LogManager.Utils.Diagnostics;

namespace LogManager.TestUtility
{
    public class WpfBindingTester
    {
        /// <summary>load a view in a hidden window and monitor it for binding errors</summary>
        /// <param name="view">a data-bound view to load and monitor for binding errors</param>
        public static void CheckViewBindings(object view)
        {
            using (BindingErrorListener listener = new BindingErrorListener())
            {
                ManualResetEventSlim mre = new ManualResetEventSlim(false);

                var window = new Window
                {
                    Width = 0,
                    Height = 0,
                    WindowStyle = WindowStyle.None,
                    ShowInTaskbar = false,
                    ShowActivated = false,
                    Content = view
                };

                window.Loaded += (o, e) => mre.Set();
                window.Show();

                mre.Wait();

                window.Close();

                if (!string.IsNullOrEmpty(listener.Errors))
                    throw new Exception("Binding Errors :\n" + listener.Errors);
            }
        }
    }
}
